/* MOS                                                        */
/* Giordano Mion, Luca David Opromolla and Alessandro Sforza  */
/* University of Sussex, Banco de Portugal & NC State, and Bologna       */

program drop _all
clear all
clear matrix
capture log close
set more off
local path = "/Users/lucadavidopromolla/Projects/MOS_rev"
cd "`path'/computation"
log using "`path'/logs/MOS_entry_May_4_2022_rev.log", replace
set mem 12g
set matsize 7500


/////////////////////////////////////
//////////// Prepare data ///////////
/////////////////////////////////////


// 1. Prepare data for firm-destination-year regressions

// Load data and merge with common dataset (to use same set of observations in all regressions, by product and by destination)
use MMT_trade_status_entryb_s.dta, clear
merge m:1 empresa ano using common_dataset
keep if _merge==3
drop _merge
cap drop __000000
sort empresa ano
merge m:1 empresa ano using warexp_data
keep if _merge==3
drop _merge
label var warexp "Mg. w/ Conflict Exp. (0/1)"
egen firm_year_tag = tag(empresa ano)
save ang,replace


// Standardize variables (using the common sample standard deviation at the firm-year-level for firm-year variables and at the firm-year-destination-level for specific export experience)
local ctr_exp_mfs = "relsize relapp_lbprod ln_firmage foreign sh_skilled relmean_age relsd_age relmean_educ relsd_educ"
foreach x of varlist `ctr_exp_mfs' {
sum `x' if firm_year_tag==1
replace `x' = `x'/r(sd)
}
save MMT_trade_status_entryb_basic,replace

// Balanced sample 
keep if dest==8
egen nobs = count(ano),by(empresa)
tab nobs
keep if nobs==10
keep empresa
sort empresa
save bsample,replace

// Prepare Entry Dummy and Other Variables
use MMT_trade_status_entryb_basic,clear
sort empresa dest ano
gen entry = (de_==1 & de_[_n-1]==0 & empresa==empresa[_n-1] & dest==dest[_n-1] & other_exporter==0) // Keep only period in which the firm can enter into a new destination
egen min_entry = min(ano),by(empresa dest entry)
gen min_entry_t = min_entry if entry==1
egen min_entry2 = min(min_entry_t),by(empresa dest)
gen entry_bs = entry
replace entry_bs = 1 if ano>=min_entry2
drop min_entry min_entry2 min_entry_t
sort empresa dest ano
gen lmkt_spec_exp = mkt_spec_exp[_n-1] if dest==dest[_n-1] & empresa==empresa[_n-1]
sort empresa dest ano
gen l2mkt_spec_exp = mkt_spec_exp[_n-2] if dest==dest[_n-2] & empresa==empresa[_n-2]
sort empresa dest ano
gen l3mkt_spec_exp = mkt_spec_exp[_n-3] if dest==dest[_n-3] & empresa==empresa[_n-3]
sort empresa dest ano
gen lwarexp = warexp[_n-1] if dest==dest[_n-1] & empresa==empresa[_n-1]
sort empresa dest ano
gen l2warexp = warexp[_n-2] if dest==dest[_n-2] & empresa==empresa[_n-2]
sort empresa dest ano
gen l3warexp = warexp[_n-3] if dest==dest[_n-3] & empresa==empresa[_n-3]
sort empresa dest ano

sort empresa dest ano
forvalues i=1997/2005 {
gen byte y`i' = ano==`i'
gen byte y`i'_ang = (ano==`i')*(dest==8)
gen inta`i' = mkt_spec_exp*(ano==`i')
gen linta`i' = lmkt_spec_exp*(ano==`i')
gen inta`i'_ang = mkt_spec_exp*(ano==`i')*(dest==8)
gen inta`i'_oect = mkt_spec_exp*(ano==`i')*(dest==9 | dest==10 | dest==11 | dest==12 | dest==13 | dest==14)
egen inta`i'_oec = max(inta`i'_oect),by(empresa ano)
gen linta`i'_ang = lmkt_spec_exp*(ano==`i')*(dest==8)
gen l2inta`i' = l2mkt_spec_exp*(ano==`i')
gen l2inta`i'_ang = l2mkt_spec_exp*(ano==`i')*(dest==8)
gen l3inta`i' = l3mkt_spec_exp*(ano==`i')
gen l3inta`i'_ang = l3mkt_spec_exp*(ano==`i')*(dest==8)
gen winta`i' = warexp*(ano==`i')
gen lwinta`i' = lwarexp*(ano==`i')
gen l2winta`i' = l2warexp*(ano==`i')
gen l3winta`i' = l3warexp*(ano==`i')
label var inta`i'_ang "Manag. w/ Angola Exp. * Year=`i' (0/1)"
label var winta`i' "Mg. w/ Conflict Exp. * Year=`i' (0/1)"
}

gen inta_bf2002 = mkt_spec_exp*(ano>=2002)
gen linta_bf2002 = lmkt_spec_exp*(ano>=2002)
gen inta_bf2002_ang = mkt_spec_exp*(ano>=2002)*(dest==8)
gen inta_bf2002_oect = mkt_spec_exp*(ano>=2002)*(dest==9 | dest==10 | dest==11 | dest==12 | dest==13 | dest==14)
egen inta_bf2002_oec = max(inta2002_oect),by(empresa ano)
gen linta_bf2002_ang = lmkt_spec_exp*(ano>=2002)*(dest==8)
gen l2inta_bf2002 = l2mkt_spec_exp*(ano>=2002)
gen l2inta_bf2002_ang = l2mkt_spec_exp*(ano>=2002)*(dest==8)
gen l3inta_bf2002 = l3mkt_spec_exp*(ano>=2002)
gen l3inta_bf2002_ang = l3mkt_spec_exp*(ano>=2002)*(dest==8)
gen winta_bf2002 = warexp*(ano>=2002)
gen lwinta_bf2002 = lwarexp*(ano>=2002)
gen l2winta_bf2002 = l2warexp*(ano>=2002)
gen l3winta_bf2002 = l3warexp*(ano>=2002)
label var inta_bf2002_ang "Manag. w/ Angola Exp. * Year>=2002 (0/1)"
label var winta_bf2002 "Mg. w/ Conflict Exp. * Year>=2002 (0/1)"


gen mkt_spec_exp_ang = mkt_spec_exp*(dest==8)
gen mkt_spec_exp_oect = mkt_spec_exp*(dest==9 | dest==10 | dest==11 | dest==12 | dest==13 | dest==14)
egen mkt_spec_exp_oec = max(mkt_spec_exp_oec),by(empresa ano)
label var mkt_spec_exp_ang "Manag. w/ Angola Exp."
gen ang = dest==8
gen lmkt_spec_exp_ang = lmkt_spec_exp*(dest==8)
gen l2mkt_spec_exp_ang = l2mkt_spec_exp*(dest==8)
gen l3mkt_spec_exp_ang = l3mkt_spec_exp*(dest==8)
gen inta20023 = mkt_spec_exp*(ano>=2002)*(ano<=2003)
gen inta20045 = mkt_spec_exp*(ano>=2004)*(ano<=2005)
gen linta20023 = lmkt_spec_exp*(ano>=2002)*(ano<=2003)
gen linta20045 = lmkt_spec_exp*(ano>=2004)*(ano<=2005)
gen l2inta20023 = l2mkt_spec_exp*(ano>=2002)*(ano<=2003)
gen l2inta20045 = l2mkt_spec_exp*(ano>=2004)*(ano<=2005)
gen l3inta20023 = l3mkt_spec_exp*(ano>=2002)*(ano<=2003)
gen l3inta20045 = l3mkt_spec_exp*(ano>=2004)*(ano<=2005)

forvalues i=1997/2005 {
label var y`i' "Year `i' (0/1)"
label var inta`i' "Year=`i' * Manag. w/ Spec. Exp. (0/1)"
}
egen empresa_time = group(empresa ano)
gen od = dest!=8 & dest>=1 & dest<=14 & de_==1
egen nod = sum(od),by(empresa ano)
sort dest empresa ano
gen lnod = nod[_n-1] if empresa==empresa[_n-1] & dest==dest[_n-1]
tab dest_time,gen(yrd)

// Keep Firm-Year Pairs with no exports in t-1 + raw regression
sort empresa dest ano
keep if (de_[_n-1]==0 & empresa==empresa[_n-1] & dest==dest[_n-1] & other_exporter==0)
reg entry mkt_spec_exp if dest==8 // Average entry rate in Angola without manager with experience 4.0%, with manager 5.9% (difference significant at 1%)

// Prepare Samples
//use MMT_trade_status_entryb_s1.dta, clear
keep if ano>=1997 & ano<=2005
keep if dest==8 | dest==9 | dest==10 | dest==11 | dest==12 | dest==13 | dest==14
xtset empresa_time dest
save data_excol,replace
keep if dest==8
xtset empresa ano
save data_ang,replace
keep empresa ano
save dataset_ang,replace


/////////////////////////////////////
//////////// Regressions ////////////
/////////////////////////////////////

// Locals
local instruct "tex(pretty frag) tdec(3) rdec(3) auto(3) bdec(3) sdec(3) symbol($^a$,$^b$,$^c$) se label  e(N) nocons"
local ctr_exp_mf = "relsize relapp_lbprod ln_firmage foreign lnod sh_skilled sh_skilled_d relmean_age relsd_age relmean_educ relsd_educ"
local y "y1999 y2000 y2002 y2003 y2004 y2005"
local inta2002 "inta_bf2002"
local inta "inta2002 inta2003 inta2004 inta2005"
local inta_sim "inta20023 inta20045"
local linta_sim "linta20023 linta20045"
local l2inta_sim "l2inta20023 l2inta20045"
local l3inta_sim "l3inta20023 l3inta20045"
local linta "linta2002 linta2003 linta2004 linta2005"
local linta2002 "linta_bf2002"
local linta2002_ang "linta_bf2002_ang"
local y_ang "y1998_ang y1999_ang y2000_ang y2002_ang y2003_ang y2004_ang y2005_ang"
local inta_ang "inta2002_ang inta2003_ang inta2004_ang inta2005_ang"
local linta_ang "linta2002_ang linta2003_ang linta2004_ang linta2005_ang"
local l2inta "l2inta2002 l2inta2003 l2inta2004 l2inta2005"
local l2inta2002 "l2inta_bf2002"
local l2inta2002_ang "l2inta_bf2002_ang"
local l2inta_ang "l2inta2002_ang l2inta2003_ang l2inta2004_ang l2inta2005_ang"
local l3inta "l3inta2002 l3inta2003 l3inta2004 l3inta2005"
local l3inta2002 "l3inta_bf2002"
local l3inta2002_ang "l3inta_bf2002_ang"
local l3inta_ang "l3inta2002_ang l3inta2003_ang l3inta2004_ang l3inta2005_ang"
local winta2002 "winta_bf2002"
local winta "winta2002 winta2003 winta2004 winta2005"
local lwinta "lwinta2002 lwinta2003 lwinta2004 lwinta2005"
local l2winta "l2winta2002 l2winta2003 l2winta2004 l2winta2005"
local l3winta "l3winta2002 l3winta2003 l3winta2004 l3winta2005"
local inta2002_ang "inta_bf2002_ang"

cd "`path'/results"


// A. Destination experience
////////////////////////////

// Full-history experience

// A1. Having a manager with export experience to Angola

// Regular sample
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
reg entry mkt_spec_exp `y' `ctr_exp_mf' i.nace,  vce(cluster empresa)
estat summarize
outreg2 using tables/table1dw_ba.xls, ctitle(OLS) addtext("Firm-time controls", X, "Year FE", X, "Industry FE", X, "Estimation Method", "OLS") `instruct' replace keep(mkt_spec_exp) 
outreg2 using tables/table1dw_ctr_ba.xls, ctitle(OLS) `instruct' replace keep(`ctr_exp_mf' `y')

// Balanced sample
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
sort empresa
merge empresa using "`path'/computation/bsample.dta"
keep if _merge==3
drop _merge
reg entry_bs mkt_spec_exp `y' `ctr_exp_mf' i.nace,  vce(cluster empresa)
estat summarize
outreg2 using tables/table1dw_bsample_ba.xls, ctitle(OLS) addtext("Firm-time controls", X, "Year FE", X, "Industry FE", X, "Estimation Method", "OLS") `instruct' replace keep(mkt_spec_exp) 
outreg2 using tables/table1dw_ctr_bsample_ba.xls, ctitle(OLS) `instruct' replace keep(`ctr_exp_mf' `y')


// A2. Placebo: effect of export experience before the end of the war


// Regular sample
	// Dynamic diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
reghdfe entry mkt_spec_exp `y' `inta' `ctr_exp_mf', absorb(empresa) vce(cluster empresa) tol(0.001)
outreg2 using tables/table1dw_ba.xls, ctitle("Placebo") addtext("Firm-time controls", X,"Firm FE", X, "Year FE", X, "Estimation Method", "OLS") `instruct' append keep(mkt_spec_exp `inta') 
outreg2 using tables/table1dw_ctr_ba.xls, ctitle("Placebo") `instruct' append keep(`ctr_exp_mf' `y')
estat summarize
	// Diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
reghdfe entry mkt_spec_exp `y' `inta2002' `ctr_exp_mf', absorb(empresa) vce(cluster empresa) tol(0.001)
outreg2 using tables/table1dw_ba.xls, ctitle("Placebo") addtext("Firm-time controls", X,"Firm FE", X, "Year FE", X, "Estimation Method", "OLS") `instruct' append keep(mkt_spec_exp `inta2002') 
outreg2 using tables/table1dw_ctr_ba.xls, ctitle("Placebo") `instruct' append keep(`ctr_exp_mf' `y')
estat summarize
	// Dynamic diff-in-diff, without firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
reg entry mkt_spec_exp `y' `inta' `ctr_exp_mf', vce(cluster empresa)
outreg2 using tables/table1dw_ba.xls, ctitle("Placebo") addtext("Firm-time controls", X, "Year FE", X, "Estimation Method", "OLS") `instruct' append keep(mkt_spec_exp `inta') 
outreg2 using tables/table1dw_ctr_ba.xls, ctitle("Placebo") `instruct' append keep(`ctr_exp_mf' `y')
estat summarize
	// Diff-in-diff, without firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
reg entry mkt_spec_exp `y' `inta2002' `ctr_exp_mf', vce(cluster empresa)
outreg2 using tables/table1dw_ba.xls, ctitle("Placebo") addtext("Firm-time controls", X, "Year FE", X, "Estimation Method", "OLS") `instruct' append keep(mkt_spec_exp `inta2002') 
outreg2 using tables/table1dw_ctr_ba.xls, ctitle("Placebo") `instruct' append keep(`ctr_exp_mf' `y')
	// Simplified dynamic diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
reghdfe entry mkt_spec_exp `y' `inta_sim' `ctr_exp_mf', absorb(empresa) vce(cluster empresa) tol(0.001)
outreg2 using tables/table1dw_ba_sim.xls, ctitle("Placebo") addtext("Firm-time controls", X,"Firm FE", X, "Year FE", X, "Estimation Method", "OLS") `instruct' append keep(mkt_spec_exp `inta_sim') 
outreg2 using tables/table1dw_ctr_ba_sim.xls, ctitle("Placebo") `instruct' append keep(`ctr_exp_mf' `y')
estat summarize
	// Simplified dynamic diff-in-diff, without firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
reg entry mkt_spec_exp `y' `inta_sim' `ctr_exp_mf', vce(cluster empresa)
outreg2 using tables/table1dw_ba_sim.xls, ctitle("Placebo") addtext("Firm-time controls", X, "Year FE", X, "Estimation Method", "OLS") `instruct' append keep(mkt_spec_exp `inta_sim') 
outreg2 using tables/table1dw_ctr_ba_sim.xls, ctitle("Placebo") `instruct' append keep(`ctr_exp_mf' `y')
	
// Balanced sample
	// Dynamic diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
sort empresa
merge empresa using "`path'/computation/bsample.dta"
keep if _merge==3
drop _merge
reghdfe entry_bs mkt_spec_exp `y' `inta' `ctr_exp_mf', absorb(empresa) vce(cluster empresa) tol(0.001)
outreg2 using tables/table1dw_bsample_ba.xls, ctitle("Placebo") addtext("Firm-time controls", X,"Firm FE", X, "Year FE", X, "Estimation Method", "OLS") `instruct' append keep(mkt_spec_exp `inta') 
outreg2 using tables/table1dw_ctr_bsample_ba.xls, ctitle("Placebo") `instruct' append keep(`ctr_exp_mf' `y')
estat summarize
	// Diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
sort empresa
merge empresa using "`path'/computation/bsample.dta"
keep if _merge==3
drop _merge
reghdfe entry_bs mkt_spec_exp `y' `inta2002' `ctr_exp_mf', absorb(empresa) vce(cluster empresa) tol(0.001)
outreg2 using tables/table1dw_bsample_ba.xls, ctitle("Placebo") addtext("Firm-time controls", X,"Firm FE", X, "Year FE", X, "Estimation Method", "OLS") `instruct' append keep(mkt_spec_exp `inta2002') 
outreg2 using tables/table1dw_ctr_bsample_ba.xls, ctitle("Placebo") `instruct' append keep(`ctr_exp_mf' `y')
estat summarize
	// Dynamic diff-in-diff, without firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
sort empresa
merge empresa using "`path'/computation/bsample.dta"
keep if _merge==3
drop _merge
reg entry_bs mkt_spec_exp `y' `inta' `ctr_exp_mf', vce(cluster empresa)
outreg2 using tables/table1dw_bsample_ba.xls, ctitle("Placebo") addtext("Firm-time controls", X, "Year FE", X, "Estimation Method", "OLS") `instruct' append keep(mkt_spec_exp `inta') 
outreg2 using tables/table1dw_ctr_bsample_ba.xls, ctitle("Placebo") `instruct' append keep(`ctr_exp_mf' `y')
estat summarize
	// Diff-in-diff, without firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
sort empresa
merge empresa using "`path'/computation/bsample.dta"
keep if _merge==3
drop _merge
reg entry_bs mkt_spec_exp `y' `inta2002' `ctr_exp_mf', vce(cluster empresa)
outreg2 using tables/table1dw_bsample_ba.xls, ctitle("Placebo") addtext("Firm-time controls", X, "Year FE", X, "Estimation Method", "OLS") `instruct' append keep(mkt_spec_exp `inta2002') 
outreg2 using tables/table1dw_ctr_bsample_ba.xls, ctitle("Placebo") `instruct' append keep(`ctr_exp_mf' `y')
estat summarize
	// Simplified dynamic diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
sort empresa
merge empresa using "`path'/computation/bsample.dta"
keep if _merge==3
drop _merge
reghdfe entry mkt_spec_exp `y' `inta_sim' `ctr_exp_mf', absorb(empresa) vce(cluster empresa) tol(0.001)
outreg2 using tables/table1dw_bsample_ba_sim.xls, ctitle("Placebo") addtext("Firm-time controls", X,"Firm FE", X, "Year FE", X, "Estimation Method", "OLS") `instruct' replace keep(mkt_spec_exp `inta_sim') 
outreg2 using tables/table1dw_ctr_bsample_ba_sim.xls, ctitle("Placebo") `instruct' replace keep(`ctr_exp_mf' `y')
estat summarize
	// Simplified dynamic diff-in-diff, without firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
sort empresa
merge empresa using "`path'/computation/bsample.dta"
keep if _merge==3
drop _merge
reg entry mkt_spec_exp `y' `inta_sim' `ctr_exp_mf', vce(cluster empresa)
outreg2 using tables/table1dw_bsample_ba_sim.xls, ctitle("Placebo") addtext("Firm-time controls", X, "Year FE", X, "Estimation Method", "OLS") `instruct' append keep(mkt_spec_exp `inta_sim') 
outreg2 using tables/table1dw_ctr_bsample_ba_sim.xls, ctitle("Placebo") `instruct' append keep(`ctr_exp_mf' `y')

// A3. Placebo: IV

// Regular sample
	// Dynamic diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
xtivreg2 entry (mkt_spec_exp `inta' = l3mkt_spec_exp `l3inta' l2mkt_spec_exp `l2inta' lmkt_spec_exp `linta') y1999 y2000 y2002 y2003 y2004 y2005 `ctr_exp_mf',fe  cluster(empresa) first
outreg2 using tables/table1dw_ba.xls, ctitle("Placebo IV") addtext("Firm-time controls", X,"Firm FE", X, "Year FE", X, "Estimation Method", "IV") `instruct' append keep(mkt_spec_exp `inta') 
outreg2 using tables/table1dw_ctr_ba.xls, ctitle("Placebo IV") `instruct' append keep(`ctr_exp_mf' `y')
estat summarize
	// Diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
su entry mkt_spec_exp `inta2002'  l3mkt_spec_exp `l3inta' l2mkt_spec_exp `l2inta' lmkt_spec_exp `linta' y1999 y2000 y2002 y2003 y2004 y2005 `ctr_exp_mf' if ano==2002
xtivreg2 entry (mkt_spec_exp `inta2002' = l3mkt_spec_exp `l3inta2002' l2mkt_spec_exp `l2inta2002' lmkt_spec_exp `linta2002') y1999 y2000 y2002 y2003 y2004 y2005 `ctr_exp_mf',fe  cluster(empresa) first partial(y1999 y2000 y2002 y2003 y2004 y2005)
outreg2 using tables/table1dw_ba.xls, ctitle("Placebo IV") addtext("Firm-time controls", X,"Firm FE", X, "Year FE", X, "Estimation Method", "IV") `instruct' append keep(mkt_spec_exp `inta2002') 
outreg2 using tables/table1dw_ctr_ba.xls, ctitle("Placebo IV") `instruct' append keep(`ctr_exp_mf' `y')
estat summarize
	// Dynamic diff-in-diff, without firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
ivreg2 entry (mkt_spec_exp `inta' = l3mkt_spec_exp `l3inta' l2mkt_spec_exp `l2inta' lmkt_spec_exp `linta') y1999 y2000 y2002 y2003 y2004 y2005 `ctr_exp_mf',  cluster(empresa) first
outreg2 using tables/table1dw_ba.xls, ctitle("Placebo IV") addtext("Firm-time controls", X, "Year FE", X, "Estimation Method", "IV") `instruct' append keep(mkt_spec_exp `inta') 
outreg2 using tables/table1dw_ctr_ba.xls, ctitle("Placebo IV") `instruct' append keep(`ctr_exp_mf' `y')
estat summarize
	// Diff-in-diff, without firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
ivreg2 entry (mkt_spec_exp `inta2002' = l3mkt_spec_exp `l3inta2002' l2mkt_spec_exp `l2inta2002' lmkt_spec_exp `linta2002') y1999 y2000 y2002 y2003 y2004 y2005 `ctr_exp_mf',  cluster(empresa) first
outreg2 using tables/table1dw_ba.xls, ctitle("Placebo IV") addtext("Firm-time controls", X, "Year FE", X, "Estimation Method", "IV") `instruct' append keep(mkt_spec_exp `inta2002') 
outreg2 using tables/table1dw_ctr_ba.xls, ctitle("Placebo IV") `instruct' append keep(`ctr_exp_mf' `y')
estat summarize
	// Simplified dynamic diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
xtivreg2 entry (mkt_spec_exp `inta_sim' = l3mkt_spec_exp `l3inta_sim' l2mkt_spec_exp `l2inta_sim' lmkt_spec_exp `linta_sim') y1999 y2000 y2002 y2003 y2004 y2005 `ctr_exp_mf',fe  cluster(empresa) first
outreg2 using tables/table1dw_ba_sim.xls, ctitle("Placebo IV 2") addtext("Firm-time controls", X,"Firm FE", X, "Year FE", X, "Estimation Method", "IV") `instruct' append keep(mkt_spec_exp `inta_sim') 
outreg2 using tables/table1dw_ctr_ba_sim.xls, ctitle("Placebo IV 2") `instruct' append keep(`ctr_exp_mf' `y')
estat summarize
	// Simplified dynamic diff-in-diff, without firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
ivreg2 entry (mkt_spec_exp `inta_sim' = l3mkt_spec_exp `l3inta_sim' l2mkt_spec_exp `l2inta_sim' lmkt_spec_exp `linta_sim') y1999 y2000 y2002 y2003 y2004 y2005 `ctr_exp_mf',  cluster(empresa) first
outreg2 using tables/table1dw_ba_sim.xls, ctitle("Placebo IV 2") addtext("Firm-time controls", X, "Year FE", X, "Estimation Method", "IV") `instruct' append keep(mkt_spec_exp `inta_sim') 
outreg2 using tables/table1dw_ctr_ba_sim.xls, ctitle("Placebo IV 2") `instruct' append keep(`ctr_exp_mf' `y')
estat summarize

// Balanced sample
	// Dynamic diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
sort empresa
merge empresa using "`path'/computation/bsample.dta"
keep if _merge==3
drop _merge
xtivreg2 entry_bs (mkt_spec_exp `inta' = l3mkt_spec_exp `l3inta' l2mkt_spec_exp `l2inta' lmkt_spec_exp `linta') y1999 y2000 y2002 y2003 y2004 y2005 `ctr_exp_mf',fe  cluster(empresa) first
outreg2 using tables/table1dw_bsample_ba.xls, ctitle("Placebo IV") addtext("Firm-time controls", X,"Firm FE", X, "Year FE", X, "Estimation Method", "IV") `instruct' append keep(mkt_spec_exp `inta') 
outreg2 using tables/table1dw_ctr_bsample_ba.xls, ctitle("Placebo IV") `instruct' append keep(`ctr_exp_mf' `y')
estat summarize
	// Diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
sort empresa
merge empresa using "`path'/computation/bsample.dta"
keep if _merge==3
drop _merge
xtivreg2 entry_bs (mkt_spec_exp `inta2002' = l3mkt_spec_exp `l3inta2002' l2mkt_spec_exp `l2inta2002' lmkt_spec_exp `linta2002') y1999 y2000 y2002 y2003 y2004 y2005 `ctr_exp_mf',fe  cluster(empresa) first
outreg2 using tables/table1dw_bsample_ba.xls, ctitle("Placebo IV") addtext("Firm-time controls", X,"Firm FE", X, "Year FE", X, "Estimation Method", "IV") `instruct' append keep(mkt_spec_exp `inta2002') 
outreg2 using tables/table1dw_ctr_bsample_ba.xls, ctitle("Placebo IV") `instruct' append keep(`ctr_exp_mf' `y')
estat summarize
	// Dynamic diff-in-diff, without firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
sort empresa
merge empresa using "`path'/computation/bsample.dta"
keep if _merge==3
drop _merge
ivreg2 entry_bs (mkt_spec_exp `inta' = l3mkt_spec_exp `l3inta' l2mkt_spec_exp `l2inta' lmkt_spec_exp `linta') y1999 y2000 y2002 y2003 y2004 y2005 `ctr_exp_mf',  cluster(empresa) first
outreg2 using tables/table1dw_bsample_ba.xls, ctitle("Placebo IV") addtext("Firm-time controls", X, "Year FE", X, "Estimation Method", "IV") `instruct' append keep(mkt_spec_exp `inta') 
outreg2 using tables/table1dw_ctr_bsample_ba.xls, ctitle("Placebo IV") `instruct' append keep(`ctr_exp_mf' `y')
estat summarize
	// Diff-in-diff, without firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
sort empresa
merge empresa using "`path'/computation/bsample.dta"
keep if _merge==3
drop _merge
ivreg2 entry_bs (mkt_spec_exp `inta2002' = l3mkt_spec_exp `l3inta2002' l2mkt_spec_exp `l2inta2002' lmkt_spec_exp `linta2002') y1999 y2000 y2002 y2003 y2004 y2005 `ctr_exp_mf',  cluster(empresa) first
outreg2 using tables/table1dw_bsample_ba.xls, ctitle("Placebo IV") addtext("Firm-time controls", X,"Firm FE", X, "Year FE", X, "Estimation Method", "IV") `instruct' append keep(mkt_spec_exp `inta2002') 
outreg2 using tables/table1dw_ctr_bsample_ba.xls, ctitle("Placebo IV") `instruct' append keep(`ctr_exp_mf' `y')
estat summarize
	// Simplified dynamic diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
sort empresa
merge empresa using "`path'/computation/bsample.dta"
keep if _merge==3
drop _merge
xtivreg2 entry_bs (mkt_spec_exp `inta_sim' = l3mkt_spec_exp `l3inta_sim' l2mkt_spec_exp `l2inta_sim' lmkt_spec_exp `linta_sim') y1999 y2000 y2002 y2003 y2004 y2005 `ctr_exp_mf',fe  cluster(empresa) first
outreg2 using tables/table1dw_bsample_ba_sim.xls, ctitle("Placebo IV 2") addtext("Firm-time controls", X,"Firm FE", X, "Year FE", X, "Estimation Method", "IV") `instruct' append keep(mkt_spec_exp `inta_sim') 
outreg2 using tables/table1dw_ctr_bsample_ba_sim.xls, ctitle("Placebo IV 2") `instruct' append keep(`ctr_exp_mf' `y')
estat summarize
	// Simplified dynamic diff-in-diff, without firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
sort empresa
merge empresa using "`path'/computation/bsample.dta"
keep if _merge==3
drop _merge
ivreg2 entry_bs (mkt_spec_exp `inta_sim' = l3mkt_spec_exp `l3inta_sim' l2mkt_spec_exp `l2inta_sim' lmkt_spec_exp `linta_sim') y1999 y2000 y2002 y2003 y2004 y2005 `ctr_exp_mf',  cluster(empresa) first
outreg2 using tables/table1dw_bsample_ba_sim.xls, ctitle("Placebo IV 2") addtext("Firm-time controls", X, "Year FE", X, "Estimation Method", "IV") `instruct' append keep(mkt_spec_exp `inta_sim') 
outreg2 using tables/table1dw_ctr_bsample_ba_sim.xls, ctitle("Placebo IV 2") `instruct' append keep(`ctr_exp_mf' `y')
estat summarize


// A4. Placebo: OLS, also controlling for experience in conflict countries

// Regular sample
	// Dynamic diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
reghdfe entry mkt_spec_exp warexp `inta' `winta' y1998 y1999 y2000 y2002 y2003 y2004 y2005 `ctr_exp_mf',absorb(empresa) vce(cluster empresa) tol(0.001)
outreg2 using tables/table1dw_ba.xls, ctitle("Placebo OLS + war exp.") addtext("Firm-time controls", X,"Firm FE", X, "Year FE", X, "Estimation Method", "OLS") `instruct' append keep(mkt_spec_exp `inta' warexp `winta') 
outreg2 using tables/table1dw_ctr_ba.xls, ctitle("Placebo OLS + war exp.") `instruct' append keep(`ctr_exp_mf' `y')
estat summarize
	// Diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
reghdfe entry mkt_spec_exp warexp `inta2002' `winta2002' y1998 y1999 y2000 y2002 y2003 y2004 y2005 `ctr_exp_mf',absorb(empresa) vce(cluster empresa) tol(0.001)
outreg2 using tables/table1dw_ba.xls, ctitle("Placebo OLS + war exp.") addtext("Firm-time controls", X,"Firm FE", X, "Year FE", X, "Estimation Method", "OLS") `instruct' append keep(mkt_spec_exp `inta2002' warexp `winta2002') 
outreg2 using tables/table1dw_ctr_ba.xls, ctitle("Placebo OLS + war exp.") `instruct' append keep(`ctr_exp_mf' `y')
estat summarize

// Balanced sample
	// Dynamic diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
sort empresa
merge empresa using "`path'/computation/bsample.dta"
keep if _merge==3
drop _merge
reghdfe entry_bs mkt_spec_exp warexp `inta' `winta' y1998 y1999 y2000 y2002 y2003 y2004 y2005 `ctr_exp_mf',absorb(empresa) vce(cluster empresa) tol(0.001)
outreg2 using tables/table1dw_bsample_ba.xls, ctitle("Placebo OLS + war exp.") addtext("Firm-time controls", X,"Firm FE", X, "Year FE", X, "Estimation Method", "OLS") `instruct' append keep(mkt_spec_exp `inta' warexp `winta') 
outreg2 using tables/table1dw_ctr_bsample_ba.xls, ctitle("Placebo OLS + war exp.") `instruct' append keep(`ctr_exp_mf' `y')
estat summarize
	// Diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
sort empresa
merge empresa using "`path'/computation/bsample.dta"
keep if _merge==3
drop _merge
reghdfe entry_bs mkt_spec_exp warexp `inta2002' `winta2002' y1998 y1999 y2000 y2002 y2003 y2004 y2005 `ctr_exp_mf',absorb(empresa) vce(cluster empresa) tol(0.001)
outreg2 using tables/table1dw_bsample_ba.xls, ctitle("Placebo OLS + war exp.") addtext("Firm-time controls", X,"Firm FE", X, "Year FE", X, "Estimation Method", "OLS") `instruct' append keep(mkt_spec_exp `inta2002' warexp `winta2002') 
outreg2 using tables/table1dw_ctr_bsample_ba.xls, ctitle("Placebo OLS + war exp.") `instruct' append keep(`ctr_exp_mf' `y')
estat summarize


// A6. OLS with firm-time FE, use all ex-colonies

// Regular sample
	// Dynamic diff-in-diff, with firm fixed effects
use "`path'/computation/data_excol",clear
keep if ano>=1998 & ano<=2005
reghdfe entry mkt_spec_exp mkt_spec_exp_ang `inta' `inta_ang', absorb(firm_time)  cluster(empresa_time)  tol(0.001)
outreg2 using tables/table1dw_ba.xls, ctitle("Firm-Time FE") addtext("Firm-Year FE", X, "Destination-Year FE", X, "OLS", X) `instruct' append keep(mkt_spec_exp mkt_spec_exp_ang `inta' `inta_ang') 
outreg2 using tables/table1dw_ctr_ba.xls, ctitle("Firm-Time FE") `instruct' append keep(`ctr_exp_mf')
estat summarize
	// Diff-in-diff, with firm fixed effects
use "`path'/computation/data_excol",clear
keep if ano>=1998 & ano<=2005
reghdfe entry mkt_spec_exp mkt_spec_exp_ang `inta2002' `inta2002_ang', absorb(firm_time)  cluster(empresa_time) tol(0.001)
outreg2 using tables/table1dw_ba.xls, ctitle("Firm-Time FE") addtext("Firm-Year FE", X, "Destination-Year FE", X, "OLS", X) `instruct' append keep(mkt_spec_exp mkt_spec_exp_ang `inta2002' `inta2002_ang') 
outreg2 using tables/table1dw_ctr_ba.xls, ctitle("Firm-Time FE") `instruct' append keep(`ctr_exp_mf')
estat summarize

// Balanced sample
	// Dynamic diff-in-diff, with firm fixed effects
use "`path'/computation/data_excol",clear
keep if ano>=1998 & ano<=2005
sort empresa
merge empresa using "`path'/computation/bsample.dta"
keep if _merge==3
drop _merge
reghdfe entry_bs mkt_spec_exp mkt_spec_exp_ang `inta' `inta_ang', absorb(firm_time)  cluster(empresa_time)  tol(0.001)
outreg2 using tables/table1dw_bsample_ba.xls, ctitle("Firm-Time FE") addtext("Firm-Year FE", X, "Destination-Year FE", X, "OLS", X) `instruct' append keep(mkt_spec_exp mkt_spec_exp_ang `inta' `inta_ang') 
outreg2 using tables/table1dw_ctr_bsample_ba.xls, ctitle("Firm-Time FE") `instruct' append keep(`ctr_exp_mf')
estat summarize
	// Diff-in-diff, with firm fixed effects
use "`path'/computation/data_excol",clear
keep if ano>=1998 & ano<=2005
sort empresa
merge empresa using "`path'/computation/bsample.dta"
keep if _merge==3
drop _merge
reghdfe entry_bs mkt_spec_exp mkt_spec_exp_ang `inta2002' `inta2002_ang', absorb(firm_time)  cluster(empresa_time)  tol(0.001)
outreg2 using tables/table1dw_bsample_ba.xls, ctitle("Firm-Time FE") addtext("Firm-Year FE", X, "Destination-Year FE", X, "OLS", X) `instruct' append keep(mkt_spec_exp mkt_spec_exp_ang `inta2002' `inta2002_ang') 
outreg2 using tables/table1dw_ctr_bsample_ba.xls, ctitle("Firm-Time FE") `instruct' append keep(`ctr_exp_mf')
estat summarize


// A7. OLS, also use experience in other ex-colonies

// Regular sample
	// Dynamic diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang.dta",clear
local inta_oec "inta1998_oec inta1999_oec inta2000_oec inta2002_oec inta2003_oec inta2004_oec inta2005_oec"
local inta2002_oec "inta2002_oec"
keep if ano>=1998 & ano<=2005
reghdfe entry mkt_spec_exp mkt_spec_exp_oec `inta' `inta_oec' `y' `ctr_exp_mf', absorb(empresa)  cluster(empresa) tol(0.001)
outreg2 using tables/table1dw_ba.xls, ctitle("Exp Other Col") addtext("Firm FE", X, "OLS", X) `instruct' append keep(mkt_spec_exp mkt_spec_exp_oec `inta' `inta_oec') 
outreg2 using tables/table1dw_ctr_ba.xls, ctitle("Exp Other Col") `instruct' append keep(`y' `ctr_exp_mf')
estat summarize
	// Diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
local inta_oec "inta1998_oec inta1999_oec inta2000_oec inta2002_oec inta2003_oec inta2004_oec inta2005_oec"
local inta2002_oec "inta2002_oec"
reghdfe entry mkt_spec_exp mkt_spec_exp_oec `inta2002' `inta2002_oec' `y' `ctr_exp_mf', absorb(empresa)  cluster(empresa) tol(0.001)
outreg2 using tables/table1dw_ba.xls, ctitle("Exp Other Col") addtext("Firm FE", X, "OLS", X) `instruct' append keep(mkt_spec_exp mkt_spec_exp_oec `inta2002' `inta2002_oec') 
outreg2 using tables/table1dw_ctr_ba.xls, ctitle("Exp Other Col") `instruct' append keep( `y' `ctr_exp_mf')
estat summarize
	// Dynamic diff-in-diff, without firm fixed effects
use "`path'/computation/data_ang.dta",clear
local inta_oec "inta1998_oec inta1999_oec inta2000_oec inta2002_oec inta2003_oec inta2004_oec inta2005_oec"
local inta2002_oec "inta2002_oec"
keep if ano>=1998 & ano<=2005
reg entry mkt_spec_exp mkt_spec_exp_oec `inta' `inta_oec' `y' `ctr_exp_mf', cluster(empresa)
outreg2 using tables/table1dw_ba.xls, ctitle("Exp Other Col") addtext("OLS", X) `instruct' append keep(mkt_spec_exp mkt_spec_exp_oec `inta' `inta_oec') 
outreg2 using tables/table1dw_ctr_ba.xls, ctitle("Exp Other Col") `instruct' append keep( `y' `ctr_exp_mf')
estat summarize
	// Diff-in-diff, without firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
local inta_oec "inta1998_oec inta1999_oec inta2000_oec inta2002_oec inta2003_oec inta2004_oec inta2005_oec"
local inta2002_oec "inta2002_oec"
reg entry mkt_spec_exp mkt_spec_exp_oec `inta2002' `inta2002_oec' `y' `ctr_exp_mf', cluster(empresa)
outreg2 using tables/table1dw_ba.xls, ctitle("Exp Other Col") addtext("OLS", X) `instruct' append keep(mkt_spec_exp mkt_spec_exp_oec `inta2002' `inta2002_oec') 
outreg2 using tables/table1dw_ctr_ba.xls, ctitle("Exp Other Col") `instruct' append keep( `y' `ctr_exp_mf')
estat summarize

// Balanced sample
	// Dynamic diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
local inta_oec "inta1998_oec inta1999_oec inta2000_oec inta2002_oec inta2003_oec inta2004_oec inta2005_oec"
local inta2002_oec "inta2002_oec"
sort empresa
merge empresa using "`path'/computation/bsample.dta"
keep if _merge==3
drop _merge
reghdfe entry_bs mkt_spec_exp mkt_spec_exp_oec `inta' `inta_oec' `y' `ctr_exp_mf', absorb(empresa)  cluster(empresa) tol(0.001)
outreg2 using tables/table1dw_bsample_ba.xls, ctitle("Exp Other Col") addtext("Firm FE", X,"OLS", X) `instruct' append keep(mkt_spec_exp mkt_spec_exp_oec `inta' `inta_oec') 
outreg2 using tables/table1dw_ctr_bsample_ba.xls, ctitle("Exp Other Col") `instruct' append keep( `y' `ctr_exp_mf')
estat summarize
	// Diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
local inta_oec "inta1998_oec inta1999_oec inta2000_oec inta2002_oec inta2003_oec inta2004_oec inta2005_oec"
local inta2002_oec "inta2002_oec"
sort empresa
merge empresa using "`path'/computation/bsample.dta"
keep if _merge==3
drop _merge
reghdfe entry_bs mkt_spec_exp mkt_spec_exp_oec `inta2002' `inta2002_oec' `y' `ctr_exp_mf', absorb(empresa)  cluster(empresa)  tol(0.001)
outreg2 using tables/table1dw_bsample_ba.xls, ctitle("Exp Other Col") addtext("Firm FE", X,"OLS", X) `instruct' append keep(mkt_spec_exp mkt_spec_exp_oec `inta2002' `inta2002_oec') 
outreg2 using tables/table1dw_ctr_bsample_ba.xls, ctitle("Exp Other Col") `instruct' append keep( `y' `ctr_exp_mf')
estat summarize
	// Dynamic diff-in-diff, without firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
local inta_oec "inta1998_oec inta1999_oec inta2000_oec inta2002_oec inta2003_oec inta2004_oec inta2005_oec"
local inta2002_oec "inta2002_oec"
sort empresa
merge empresa using "`path'/computation/bsample.dta"
keep if _merge==3
drop _merge
reg entry_bs mkt_spec_exp mkt_spec_exp_oec `inta' `inta_oec' `y' `ctr_exp_mf',cluster(empresa)
outreg2 using tables/table1dw_bsample_ba.xls, ctitle("Exp Other Col") addtext("OLS", X) `instruct' append keep(mkt_spec_exp mkt_spec_exp_oec `inta' `inta_oec') 
outreg2 using tables/table1dw_ctr_bsample_ba.xls, ctitle("Exp Other Col") `instruct' append keep( `y' `ctr_exp_mf')
estat summarize
	// Diff-in-diff, without firm fixed effects
use "`path'/computation/data_ang.dta",clear
keep if ano>=1998 & ano<=2005
local inta_oec "inta1998_oec inta1999_oec inta2000_oec inta2002_oec inta2003_oec inta2004_oec inta2005_oec"
local inta2002_oec "inta2002_oec"
sort empresa
merge empresa using "`path'/computation/bsample.dta"
keep if _merge==3
drop _merge
reg entry_bs mkt_spec_exp mkt_spec_exp_oec `inta2002' `inta2002_oec' `y' `ctr_exp_mf',cluster(empresa)
outreg2 using tables/table1dw_bsample_ba.xls, ctitle("Exp Other Col") addtext("OLS", X) `instruct' append keep(mkt_spec_exp mkt_spec_exp_oec `inta2002' `inta2002_oec') 
outreg2 using tables/table1dw_ctr_bsample_ba.xls, ctitle("Exp Other Col") `instruct' append keep( `y' `ctr_exp_mf')
estat summarize



// 2. Prepare data for firm-destination-product-year regressions

cd "`path'/computation"

// Expand to firm-year-product-destination

// 1. Create a dataset of all possible destination-product pairs
use product_destination0,clear
keep dest nc
gen var =1
collapse (max) var,by(dest nc)
drop if nc==. | dest==.
reshape wide var,i(dest) j(nc)
reshape long var,i(dest) j(nc)
keep dest nc
tab nc
tab dest
tab nc dest
sort dest
save expand_china1,replace

// 2. Determine exports for every firm-year-destination-product
use product_destination0,clear
collapse (sum) v_est2,by(empresa ano dest nc)
sort empresa ano dest nc
save expand_china2,replace

// 3. Expand firm-year-destination data to firm-year-destination-product using the dataset built at point 1
use ang,clear
sort dest
joinby dest using expand_china1

// 4. Attach exports to the firm-year-destination-product combinations with positive exports
sort empresa ano dest nc
merge 1:1 empresa ano dest nc using expand_china2
keep if _merge==3 | _merge==1
drop _merge

// 5. Set exports = 0 for all other combinations
recode v_est2 (.=0)
rename nc isic
sort empresa ano dest isic
save expdata,replace

// Get export experience in a product, and attach to firm-destination-product-year data, restrict to Angola
use MMT_trade_status_entryb_s_gr.dta,clear
keep empresa ano pr_gr prgr_spec_exp
rename pr_gr isic
sort empresa ano isic
save product_exp,replace
use expdata,clear
sort empresa ano isic
merge m:1 empresa ano isic using product_exp
keep if _merge==3
drop _merge
keep if dest==8
save expdata2,replace

// Get export experience in a product-destination for Angola, and attach to firm-destination-product-year data
use "`path'/computation/data_ang.dta",clear
keep empresa ano lnod
sort empresa ano
save lnodp,replace
use ang,clear
sort empresa ano dest
merge m:1 empresa ano using lnodp
keep if _merge==3
drop _merge
keep if dest>=311 & dest<=999
keep empresa ano mkt_spec_exp dest lnod
rename mkt_spec_exp prmk_spec_exp
rename dest isic
sort empresa ano isic
save prmk_exp,replace
use expdata2,clear
sort empresa ano isic
merge m:1 empresa ano isic using prmk_exp
keep if _merge==3
drop _merge
sort empresa ano isic
save compl_exp,replace

// Standardize variables (using the common sample standard deviation at the firm-year-level for firm-year variables and at the firm-year-product-level for specific export experience)
use compl_exp,clear
egen fy = tag(empresa ano)
local ctr_exp_mfs = "relsize relapp_lbprod ln_firmage foreign lnod sh_skilled relmean_age relsd_age relmean_educ relsd_educ"
foreach x of varlist `ctr_exp_mfs' {
sum `x' if fy==1
replace `x' = `x'/r(sd)
}
save compl_exp2,replace


// Prepare Entry Dummy and Other Variables
use compl_exp2,clear
drop de_
gen de_ = v_est2>0 & v_est2!=.
sort empresa isic ano
gen entry = (de_==1 & de_[_n-1]==0 & empresa==empresa[_n-1] & isic==isic[_n-1] & other_exporter==0) // Keep only period in which the firm can enter into a new destination
sort empresa isic ano
gen entry2 = (de_[_n+1]==1 & de_==1 & de_[_n-1]==0 & empresa[_n+1]==empresa & empresa==empresa[_n-1] & isic[_n+1]==isic & isic==isic[_n-1] & other_exporter==0) // Keep only period in which the firm can enter into a new destination
gen lmkt_spec_exp = mkt_spec_exp[_n-1] if isic==isic[_n-1] & empresa==empresa[_n-1]
sort empresa isic ano
gen l2mkt_spec_exp = mkt_spec_exp[_n-2] if isic==isic[_n-2] & empresa==empresa[_n-2]
sort empresa isic ano
gen l3mkt_spec_exp = mkt_spec_exp[_n-3] if isic==isic[_n-3] & empresa==empresa[_n-3]
sort empresa isic ano
gen lprgr_spec_exp = prgr_spec_exp[_n-1] if isic==isic[_n-1] & empresa==empresa[_n-1]
sort empresa isic ano
gen l2prgr_spec_exp = prgr_spec_exp[_n-2] if isic==isic[_n-2] & empresa==empresa[_n-2]
sort empresa isic ano
gen l3prgr_spec_exp = prgr_spec_exp[_n-3] if isic==isic[_n-3] & empresa==empresa[_n-3]
sort empresa isic ano
gen lprmk_spec_exp = prmk_spec_exp[_n-1] if isic==isic[_n-1] & empresa==empresa[_n-1]
sort empresa isic ano
gen l2prmk_spec_exp = prmk_spec_exp[_n-2] if isic==isic[_n-2] & empresa==empresa[_n-2]
sort empresa isic ano
gen l3prmk_spec_exp = prmk_spec_exp[_n-3] if isic==isic[_n-3] & empresa==empresa[_n-3]

forvalues i=1997/2005 {
gen byte y`i' = ano==`i'
gen inta`i' = mkt_spec_exp*(ano==`i')
gen pinta`i' = prgr_spec_exp*(ano==`i')
gen pminta`i' = prmk_spec_exp*(ano==`i')
gen linta`i' = lmkt_spec_exp*(ano==`i')
gen l2inta`i' = l2mkt_spec_exp*(ano==`i')
gen l3inta`i' = l3mkt_spec_exp*(ano==`i')
gen lpinta`i' = lprgr_spec_exp*(ano==`i')
gen l2pinta`i' = l2prgr_spec_exp*(ano==`i')
gen l3pinta`i' = l3prgr_spec_exp*(ano==`i')
gen lpminta`i' = lprmk_spec_exp*(ano==`i')
gen l2pminta`i' = l2prmk_spec_exp*(ano==`i')
gen l3pminta`i' = l3prmk_spec_exp*(ano==`i')
}

gen inta_bf2002 = mkt_spec_exp*(ano>=2002)
gen pinta_bf2002 = prgr_spec_exp*(ano>=2002)
gen pminta_bf2002 = prmk_spec_exp*(ano>=2002)
gen linta_bf2002 = lmkt_spec_exp*(ano>=2002)
gen l2inta_bf2002 = l2mkt_spec_exp*(ano>=2002)
gen l3inta_bf2002 = l3mkt_spec_exp*(ano>=2002)
gen lpinta_bf2002 = lprgr_spec_exp*(ano>=2002)
gen l2pinta_bf2002 = l2prgr_spec_exp*(ano>=2002)
gen l3pinta_bf2002 = l3prgr_spec_exp*(ano>=2002)
gen lpminta_bf2002 = lprmk_spec_exp*(ano>=2002)
gen l2pminta_bf2002 = l2prmk_spec_exp*(ano>=2002)
gen l3pminta_bf2002 = l3prmk_spec_exp*(ano>=2002)


forvalues l =1998/2005 {
label var inta`l' "Manag. w/ Dest. Exp. * Yr= `l' (0/1)"
label var pinta`l' "Manag. w/ Prod. Exp. * Yr= `l' (0/1)"
label var pminta`l' "Manag. w/ Dest*Prod Exp. * Yr= `l' (0/1)"
}
egen empresa_time = group(empresa ano)

// Keep Firm-Year Pairs with no exports in t-1 (as in regressions at the firm-year-dest level) + raw regression
sort empresa isic ano
merge m:1 empresa ano using dataset_ang
keep if _merge==3
drop _merge
reg entry mkt_spec_exp prgr_spec_exp prmk_spec_exp // Average entry rate in Angola without manager with experience 4.0%, with manager 5.9% (difference significant at 1%)
save MMT_trade_status_entryb_s1_prmk,replace

// Prepare Samples
use MMT_trade_status_entryb_s1_prmk, clear
keep if ano>=1997 & ano<=2005
egen py = group(isic ano)
tab py,gen(sy)
egen empresa_isic = group(empresa isic)
save data_ang_prmkt,replace
label var mkt_spec_exp "Manag. w/ Dest. Exp. (0/1)"
label var prgr_spec_exp "Manag. w/ Prod. Exp. (0/1)"
label var prmk_spec_exp "Manag. w/ Dest*Prod Exp. (0/1)"
save data_ang_prmkt2,replace
xtset empresa py
save data_ang_prmk,replace


// Bring in Rauch product differentiation index
preserve
use MMT_trade_status_entryb_s_gr.dta, clear
cap drop __000000
collapse (max) rauch_w,by(empresa ano pr_gr)
rename pr_gr isic
foreach x of varlist rauch_w {
sum `x'
replace `x' = `x'/r(sd)
}
sort empresa ano isic
save rauch_i,replace
restore
sort empresa ano isic
merge m:1 empresa ano isic using rauch_i
keep if _merge==3
drop _merge
foreach var of varlist mkt_spec_exp prgr_spec_exp prmk_spec_exp  inta2002 lmkt_spec_exp lprgr_spec_exp lprmk_spec_exp  linta2002 l2mkt_spec_exp l2prgr_spec_exp l2prmk_spec_exp l2inta2002 pinta2002 pminta2002 lpinta2002 lpminta2002 l2pinta2002 l2pminta2002  {
gen `var'_r = `var'*rauch_w
}
label var mkt_spec_exp_r "Manag. w/ Dest. Exp. * Prod. Diff. Index"
label var prgr_spec_exp_r "Manag. w/ Prod. Exp. * Prod. Diff. Index"
label var prmk_spec_exp_r "Manag. w/ Dest*Prod Exp. * Prod. Diff. Index"
label var inta2002_r "Mg. w/ Dest. Exp. * Yr=2002 * Prod. Diff. Index"
label var pinta2002_r "Mg. w/ Prod. Exp. * Yr=2002 * Prod. Diff. Index"
label var pminta2002_r "Mg. w/ Dest*Prod Exp. * Yr=2002 * Prod. Diff. Index"
save data_ang_prmk,replace


// C. Destination vs. product vs. product-destination experience
////////////////////////////////////////////////////////////////

// Locals
local instruct "tex(pretty frag) tdec(6) rdec(6) auto(6) bdec(6) sdec(6) symbol($^a$,$^b$,$^c$) se label  e(N) nocons"
local ctr_exp_mf = "relsize relapp_lbprod ln_firmage foreign lnod sh_skilled sh_skilled_d relmean_age relsd_age relmean_educ relsd_educ"
local y "y1999 y2000 y2002 y2003 y2004 y2005"
local inta_bf2002 "inta_bf2002 pinta_bf2002 pminta_bf2002"
local linta_bf2002 "linta_bf2002 lpinta_bf2002 lpminta_bf2002"
local l2inta_bf2002 "l2inta_bf2002 l2pinta_bf2002 l2pminta_bf2002"
local inta "inta2002 inta2003 inta2004 inta2005 pinta2002 pinta2003 pinta2004 pinta2005 pminta2002 pminta2003 pminta2004 pminta2005"
local linta "linta2002 linta2003 linta2004 linta2005 lpinta2002 lpinta2003 lpinta2004 lpinta2005 lpminta2002 lpminta2003 lpminta2004 lpminta2005"
local l2inta "l2inta2002 l2inta2003 l2inta2004 l2inta2005 l2pinta2002 l2pinta2003 l2pinta2004 l2pinta2005 l2pminta2002 l2pminta2003 l2pminta2004 l2pminta2005"
local l3inta "l3inta2002 l3inta2003 l3inta2004 l3inta2005 l3pinta2002 l3pinta2003 l3pinta2004 l3pinta2005 l3pminta2002 l3pminta2003 l3pminta2004 l3pminta2005"
local y_ang "y1998_ang y1999_ang y2000_ang y2002_ang y2003_ang y2004_ang y2005_ang"
local inta_ang "inta2002_ang inta2003_ang inta2004_ang inta2005_ang"
local linta_ang "linta2002_ang linta2003_ang linta2004_ang linta2005_ang"
local l2inta_ang "l2inta2002_ang l2inta2003_ang l2inta2004_ang l2inta2005_ang"
local inta_bf2002_r "inta_bf2002_r pinta_bf2002_r pminta_bf2002_r"
local linta_bf2002_r "linta_bf2002_r lpinta_bf2002_r lpminta_bf2002_r"
local l2inta_bf2002_r "l2inta_bf2002_r l2pinta_bf2002_r l2pminta_bf2002_r"
local linta_r "linta2002_r lpinta2002_r lpminta2002_r linta2003_r lpinta2003_r lpminta2003_r linta2004_r lpinta2004_r lpminta2004_r linta2005_r lpinta2005_r lpminta2005_r"
local l2inta_r "l2inta2002_r l2pinta2002_r l2pminta2002_r l2inta2003_r l2pinta2003_r l2pminta2003_r l2inta2004_r l2pinta2004_r l2pminta2004_r l2inta2005_r l2pinta2005_r l2pminta2005_r"
local l3inta_r "l3inta2002_r l3pinta2002_r l3pminta2002_r l3inta2003_r l3pinta2003_r l3pminta2003_r l3inta2004_r l3pinta2004_r l3pminta2004_r l3inta2005_r l3pinta2005_r l3pminta2005_r"


use "`path'/computation/data_ang.dta",clear
keep empresa ano lnod
sort empresa ano
save "`path'/computation/lnod.dta",replace
use "`path'/computation/data_ang_prmk.dta",clear
sort empresa ano
merge m:1 empresa ano using "`path'/computation/lnod.dta"
keep if _merge==3
drop _merge
save "`path'/computation/data_ang_prmk.dta",replace

cd "`path'/results"

// Full-history experience

// C1. Destination-product-specific experience, placebo, OLS
	// Dynamic diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang_prmk.dta",clear
keep if ano>=1998 & ano<=2005
reghdfe entry mkt_spec_exp prgr_spec_exp prmk_spec_exp `inta' `ctr_exp_mf' sy*,absorb(empresa) vce(cluster empresa) tol(0.001)
outreg2 using tables/table1d_pm.xls, ctitle("Destination-Product" "OLS") addtext("Firm-time controls", X, "Firm FE", X, "Industry*Year FE", X, "OLS", X) `instruct' replace keep(mkt_spec_exp prgr_spec_exp prmk_spec_exp `inta') 
outreg2 using tables/table1d_ctr_pm.xls, ctitle("Destination-Product" "OLS") `instruct' replace keep(`ctr_exp_mf')
estat summarize
	// Diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang_prmk.dta",clear
keep if ano>=1998 & ano<=2005
reghdfe entry mkt_spec_exp prgr_spec_exp prmk_spec_exp `inta_bf2002' `ctr_exp_mf' sy*,absorb(empresa) vce(cluster empresa) tol(0.001)
outreg2 using tables/table1d_pm.xls, ctitle("Destination-Product" "OLS") addtext("Firm-time controls", X, "Firm FE", X, "Industry*Year FE", X, "OLS", X) `instruct' append keep(mkt_spec_exp prgr_spec_exp prmk_spec_exp `inta_bf2002') 
outreg2 using tables/table1d_ctr_pm.xls, ctitle("Destination-Product" "OLS") `instruct' append keep(`ctr_exp_mf')
estat summarize


// C3. Destination-product-specific experience, placebo, homog. goods
	// Dynamic diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang_prmk.dta",clear
keep if ano>=1998 & ano<=2005
reghdfe entry mkt_spec_exp prgr_spec_exp prmk_spec_exp `inta' `ctr_exp_mf' sy* if rauch==0, absorb(empresa) vce(cluster empresa) tol(0.001)
outreg2 using tables/table1d_pm.xls, ctitle("Destination-Product" "Homog. Goods") addtext("Firm-time controls", X, "Firm FE", X, "Industry*Year FE", X, "OLS", X) `instruct' append keep(mkt_spec_exp prgr_spec_exp prmk_spec_exp `inta') 
outreg2 using tables/table1d_ctr_pm.xls, ctitle("Destination-Product" "Homog. Goods") `instruct' append keep(`ctr_exp_mf')
estat summarize
	// Diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang_prmk.dta",clear
keep if ano>=1998 & ano<=2005
reghdfe entry mkt_spec_exp prgr_spec_exp prmk_spec_exp `inta_bf2002' `ctr_exp_mf' sy* if rauch==0, absorb(empresa) vce(cluster empresa) tol(0.001)
outreg2 using tables/table1d_pm.xls, ctitle("Destination-Product" "Homog. Goods") addtext("Firm-time controls", X, "Firm FE", X, "Industry*Year FE", X, "OLS", X) `instruct' append keep(mkt_spec_exp prgr_spec_exp prmk_spec_exp `inta_bf2002') 
outreg2 using tables/table1d_ctr_pm.xls, ctitle("Destination-Product" "Homog. Goods") `instruct' append keep(`ctr_exp_mf')
estat summarize


// C4. Destination-product-specific experience, placebo, diff. goods
	// Dynamic diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang_prmk.dta",clear
keep if ano>=1998 & ano<=2005
reghdfe entry mkt_spec_exp prgr_spec_exp prmk_spec_exp `inta' `ctr_exp_mf' sy* if rauch>0 & rauch!=., absorb(empresa) vce(cluster empresa) tol(0.001)
outreg2 using tables/table1d_pm.xls, ctitle("Destination-Product" "Diff. Goods") addtext("Firm-time controls", X, "Firm FE", X, "Industry*Year FE", X, "OLS", X) `instruct' append keep(mkt_spec_exp prgr_spec_exp prmk_spec_exp `inta') 
outreg2 using tables/table1d_ctr_pm.xls, ctitle("Destination-Product" "Diff. Goods") `instruct' append keep(`ctr_exp_mf')
estat summarize
	// Diff-in-diff, with firm fixed effects
use "`path'/computation/data_ang_prmk.dta",clear
keep if ano>=1998 & ano<=2005
reghdfe entry mkt_spec_exp prgr_spec_exp prmk_spec_exp `inta_bf2002' `ctr_exp_mf' sy* if rauch>0 & rauch!=., absorb(empresa) vce(cluster empresa) tol(0.001)
outreg2 using tables/table1d_pm.xls, ctitle("Destination-Product" "Diff. Goods") addtext("Firm-time controls", X, "Firm FE", X, "Industry*Year FE", X, "OLS", X) `instruct' append keep(mkt_spec_exp prgr_spec_exp prmk_spec_exp `inta_bf2002') 
outreg2 using tables/table1d_ctr_pm.xls, ctitle("Destination-Product" "Diff. Goods") `instruct' append keep(`ctr_exp_mf')
estat summarize



log close
